<?php
require_once( 'Interfaces.php' );

class DB implements IDatabase
{
//  private $connect = false;

//------------------------------------------------------------------------------
  public function __construct( $config ) {
    $this->config = $config;
  }

//==============================================================================
//  operations
//==============================================================================

//------------------------------------------------------------------------------
  public function __get( $name ) {
    try{
      $this->$name();
      return $this->$name;
    }
    catch ( Exception $e ) {
      die( 'property '.$name.' is undefined. file: '.$e->getFile().', line: '.$e->getLine() );
    }
  }

//------------------------------------------------------------------------------
	public function process( $sql, $params=array() )
//------------------------------------------------------------------------------
	{
    $sql = $this->make( $sql, $params );
    $query = mysqli_query( $this->connect, $sql );
		if( !$query )
		{
//			print mysqli_error( $this->connect );
      	return false;
		}
		return true;
//		return mysqli_affected_rows( $this->connect );
	}

//------------------------------------------------------------------------------
    public function get( $sql, $params=array() )
//------------------------------------------------------------------------------
    {
      $sql = $this->make( $sql, $params );
      $out = array();
      $query = mysqli_query( $this->connect, $sql );
      if( !$query )
      {
  			$out = mysqli_error( $this->connect );
//        print_r( $out );
  			return false;
      }
      while($res = mysqli_fetch_assoc( $query ) )
      {
        $out[] = $res;
      }
      
      mysqli_free_result( $query );
      return $out;
    }

//------------------------------------------------------------------------------
	public function get_last_id( )
//------------------------------------------------------------------------------
	{
    return mysqli_insert_id( $this->connect );
	}

//------------------------------------------------------------------------------
	public function transaction( )
//------------------------------------------------------------------------------
	{
    if( !mysqli_autocommit( $this->connect, FALSE ) )
      return false;
  }
 
//------------------------------------------------------------------------------
	public function commit( )
//------------------------------------------------------------------------------
	{
    mysqli_commit( $this->connect );
    if( !mysqli_autocommit( $this->connect, TRUE ) )
      return false;
  }  

//------------------------------------------------------------------------------
	public function rollback( )
//------------------------------------------------------------------------------
	{
    mysqli_rollback( $this->connect );
    if( !mysqli_autocommit( $this->connect, TRUE ) )
      return false;
  }

//------------------------------------------------------------------------------
    public function process_array( &$sql_arr )
//------------------------------------------------------------------------------
    {
      if( count($sql_arr) == 0 )
        return false;

      if( !mysqli_autocommit( $this->connect, FALSE ) )
        return false;
        
      for($i = 0, $limit = count($sql_arr); $i < $limit; ++$i )
      {
         if( !mysqli_query( $this->connect, $sql_arr[$i] ) )
  			{
			   	mysqli_autocommit( $this->connect, TRUE );
		  		return false;
	   		}
		}
      
      if( !mysqli_commit( $this->connect ) )
      {
	     mysqli_autocommit( $this->connect, TRUE );
        return false;
		}
 
      mysqli_autocommit( $this->connect, TRUE );
      return true;
    }

//------------------------------------------------------------------------------
	public function real_escape_string( $str )
//------------------------------------------------------------------------------
	{
		return mysqli_real_escape_string( $this->connect, $str);
	}

//==============================================================================
//  implementations
//==============================================================================

//------------------------------------------------------------------------------
  private function connect() {
    if( !$this->connect ) {
      $this->connect = mysqli_connect( $this->config['host'], $this->config['user'], $this->config['pass'], $this->config['base'] );
      mysqli_query($this->connect, "SET CHARACTER SET 'utf8';" );
//      mysqli_query($this->connect, "USE ".$config['base'].";" );
    }
//    return $this->connect;
  }
  
//------------------------------------------------------------------------------
	private function make( $sql, $params )
	{
    $sql = str_replace( "%", "%%", $sql );
    $sql = str_replace( "?", "%s", $sql );
    $args = array( $sql );
    foreach( $params as $i => $v ) {
      if( is_int( $v ) )
        $args[] = $v;
      else
        $args[] = "'".mysql_escape_string($v)."'";
//      if( is_int( $v ) ) continue;  // int does not need to be escaped
//      $params[$i] = "'".mysql_escape_string($v)."'";
    }
    $values = count( $args );
    $args[ $values + 1 ] = 'UNKNOWN_PLACEHOLDER!!!';

    return call_user_func_array( 'sprintf', $args );
  }
}
?>